/*****************************************************************************
 *   uarttest.c:  main C entry file for NXP LPC23xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.13  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "lpc24xx.h"                        /* LPC23xx/24xx definitions */
#include "type.h"
#include "irq.h"
#include "uart.h"
#include <stdio.h>

extern volatile DWORD UART0Count;
extern volatile BYTE UART0Buffer[BUFSIZE];
extern volatile DWORD UART1Count;
extern volatile BYTE UART1Buffer[BUFSIZE];
volatile BYTE Buffer[BUFSIZE];
int count;

/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
#define PLL_MValue			74
#define PLL_NValue			1
#define CCLKDivValue		5
int main (void)
{
						  DWORD MValue, NValue;

  if ( PLLSTAT & (1 << 25) )
  {
	PLLCON = 1;			/* Enable PLL, disconnected */
	PLLFEED = 0xaa;
	PLLFEED = 0x55;
  }

  PLLCON = 0;				/* Disable PLL, disconnected */
  PLLFEED = 0xaa;
  PLLFEED = 0x55;
    
//  SCS |= 0x20;			/* Enable main OSC */
//  while( !(SCS & 0x40) );	/* Wait until main OSC is usable */

  CLKSRCSEL = 0x0;		/* select main OSC, 12MHz, as the PLL clock source */

  PLLCFG = PLL_MValue | (PLL_NValue << 16);
  PLLFEED = 0xaa;
  PLLFEED = 0x55;
      
  PLLCON = 1;				/* Enable PLL, disconnected */
  PLLFEED = 0xaa;
  PLLFEED = 0x55;

  CCLKCFG = CCLKDivValue;	/* Set clock divider */


  while ( ((PLLSTAT & (1 << 26)) == 0) );	/* Check lock bit status */
    
  MValue = PLLSTAT & 0x00007FFF;
  NValue = (PLLSTAT & 0x00FF0000) >> 16;
  while ((MValue != PLL_MValue) && ( NValue != PLL_NValue) );

  PLLCON = 3;				/* enable and connect */
  PLLFEED = 0xaa;
  PLLFEED = 0x55;
  while ( ((PLLSTAT & (1 << 25)) == 0) );	/* Check connect bit status */
  
  PCONP |= 0xFFFFFFFF;





	PINSEL0 |= 0x00000050;
	PINSEL4 |= 0x000A0000;       /* RxD0 and TxD0 */


    U0LCR = 0x83;		/* 8 bits, no Parity, 1 Stop bit */
 
 
    U0DLM = 0x0;							
    U0DLL = 0x2E;
	U0FDR = 0xda;
	U0LCR = 0x03;		/* DLAB = 0 */

    U0FCR = 0x07;		/* Enable and reset TX and RX FIFO. */

	PINSEL0 |= 0x00500000; //configure uart2 on pins P0.10 and P0.11
//	PCONP |= (1<<24); //enable uart2 power/clock
  	U2LCR = 0x83;
	U2DLM = 0x0;							
    U2DLL = 0x2E;
	U2FDR = 0xda;
	U2LCR = 0x03;		/* DLAB = 0 */

	U2FCR = 0x07;


	PINSEL0 |= 0x40000000;	/* Enable TxD1 P0.15 */
	PINSEL1 |= 0x00000001;	/* Enable RxD1 P0.16 */

	U1LCR = 0x83;
	U1DLM = 0x0;							
    U1DLL = 0x2E;
	U1FDR = 0xda;
	U1LCR = 0x03;		/* DLAB = 0 */

	U1FCR = 0x07;

	PINSEL1 |= 0x003C0001;

	U3LCR = 0x83;
	U3DLM = 0x0;							
    U3DLL = 0x2E;
	U3FDR = 0xda;
	U3LCR = 0x03;		/* DLAB = 0 */

	U3FCR = 0x07;
	
	printUART(2,"\001");
	printUART(2,"     ALL YOUR\r\n\r\n");
	printUART(2,"       BASE\r\n");
										

	printUART(3,"\001");
	printUART(3,"    ARE BELONG\r\n");
	printUART(3,"        TO\r\n");
	printUART(3,"        US!\r\n");
	
	
	while(1);



 	 
  return 0;
  
}

/*****************************************************************************
**                            End Of File
*****************************************************************************/
